package com.hrm.salarys.service;

import com.hrm.common.entity.PageResult;
import com.hrm.domain.salarys.UserSalary;
import com.hrm.salarys.dao.UserSalaryDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Optional;

@Service
public class SalaryService {

    @Autowired
    private UserSalaryDao userSalaryDao;


    public PageResult<Map> findList(String page, String pageSize, String companyId) {
        Page<Map> userSalaryPage = userSalaryDao.findPage(companyId, PageRequest.of(Integer.parseInt(page), Integer.parseInt(pageSize)));
        PageResult<Map> pageResult = new PageResult<>();
        pageResult.setRows(userSalaryPage.getContent());
        pageResult.setTotal(userSalaryPage.getTotalElements());
        return pageResult;
    }

    public void saveUserSalary(UserSalary userSalary){
        userSalaryDao.save(userSalary);
    }

    public UserSalary findUserSalary(String userId){
        Optional<UserSalary> byId = userSalaryDao.findById(userId);
        return byId.orElse(null);
    }
}
